package com.typy.www.tpay.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.typy.www.tpay.constance.Constance;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by 13799 on 2018/1/29.
 */

public class SDFileUtil {

    private static volatile SDFileUtil instance = null;

    public static SDFileUtil getInstance() {

        synchronized (SDFileUtil.class) {
            if (instance == null) {
                instance = new SDFileUtil();
            }
        }
        return instance;
    }

    private Context context;


    public SDFileUtil() {
    }

    public SDFileUtil(Context context) {
        super();
        this.context = context;
    }


    //Glide保存图片
    public void savePicture(Context context, final String fileName, String url, final String savePackage) {
        Glide.with(context).load(url).asBitmap().toBytes().into(new SimpleTarget<byte[]>() {
            @Override
            public void onResourceReady(byte[] bytes, GlideAnimation<? super byte[]> glideAnimation) {
                try {
                    savaFileToSD(fileName, bytes,savePackage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    //往SD卡写入文件的方法
    public void savaFileToSD(String filename, byte[] bytes, String filePath) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File dir1 = new File(filePath);
            if (!dir1.exists()) {
                dir1.mkdirs();
            }
            filename = filePath + "/" + filename;
            //这里就不要用openFileOutput了,那个是往手机内存中写数据的
            FileOutputStream output = new FileOutputStream(filename);
            output.write(bytes);
            //将bytes写入到输出流中
            output.close();
            //关闭输出流
        }
    }
    public boolean saveBitmap(Context context,Bitmap bitmap) {
        // 首先保存图片
        String pakage="";
        try {
             pakage=Environment.getExternalStorageDirectory().getCanonicalPath()+"/Zpay";
             File file=new File(pakage);
            if(!file.exists()){
                file.mkdir();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        File appDir = null;
        try {
            appDir = new File(Environment.getExternalStorageDirectory().getCanonicalPath()+"/Zpay","zxing_image");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = "zxing_image" + ".png";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // 通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + "/sdcard/namecard/")));
        if(appDir!=null&&appDir.exists()){
            return true;
        }else{
            return false;
        }
    }

}
